package com.zufangbao.earth.email.excel;

import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import com.demo2do.core.utils.DateUtils;
import com.zufangbao.sun.entity.finance.FinanceReportTitles;
import com.zufangbao.sun.entity.finance.ReportDataGenerator;
import com.zufangbao.sun.entity.finance.ReportTitle;
import com.zufangbao.sun.entity.order.OverDueReportTitles;
import com.zufangbao.sun.utils.PropertiesUtil;

@Service("excelOverDueOrderExcelService")
public class ExcelOverDueOrderExcelHandler {
	private static int TABLE_DATA_ROW_BASE_INDEX = 1;
	public String createWorkBook(List<ReportDataGenerator> reportDataGenerator,
			BigDecimal nowMoney,OverDueReportTitles[] titles,Date createDate, String companyName) throws IOException {
		
		Workbook wb = new HSSFWorkbook();
		Sheet overDueOrderReport = wb.createSheet("催收逾期款");
		HSSFCellStyle cellStyle = create_excel_title(titles, wb,
				overDueOrderReport);
		int i = create_excel_content(reportDataGenerator, wb,
				overDueOrderReport, cellStyle);
		create_excel_end_row(nowMoney, overDueOrderReport, i);
		FileOutputStream fileOut = new FileOutputStream(create_excel_file_path(createDate, companyName));
		wb.write(fileOut);
		fileOut.close();
		return create_excel_file_path(createDate, companyName);
	}

	private String create_excel_file_path(Date createDate, String companyName) {
		return PropertiesUtil.getPropertiesValue("mail.properties", "excel.location")
				+companyName+" 催收清单_"+ DateUtils.format(createDate,"yyyyMMdd") + ".v1.xls";
	}

	private void create_excel_end_row(BigDecimal nowMoney,
			Sheet overDueOrderReport, int last_row_num) {
		Row endRow = overDueOrderReport.createRow((short) last_row_num);
		Cell totalCell = endRow.createCell(6);
		totalCell.setCellValue("合计");
		Cell totalcount = endRow.createCell(7);
		totalcount.setCellValue(Double.valueOf(nowMoney.toString()));
	}

	private int create_excel_content(
			List<ReportDataGenerator> reportDataGenerator, Workbook wb,
			Sheet overDueOrderReport, HSSFCellStyle cellStyle) {
		HSSFCellStyle contentStyle = create_excel_content_style(wb, cellStyle);
		int i = 0;
		for (ReportDataGenerator financePaymentRecord : reportDataGenerator) {
			i++;
			put_content_values(overDueOrderReport, contentStyle, i,
					financePaymentRecord);
		}
		return i+TABLE_DATA_ROW_BASE_INDEX ;
	}

	private void put_content_values(Sheet overDueOrderReport,
			HSSFCellStyle contentStyle, int i,
			ReportDataGenerator financePaymentRecord) {
		Row contentRow = overDueOrderReport.createRow((short) i);
		Map<String,String> reportData=financePaymentRecord.toReportData();
		for(OverDueReportTitles title_key: OverDueReportTitles.values()){
			Cell cell = contentRow.createCell(title_key.ordinal());
			cell.setCellStyle(contentStyle);
			if(title_key.getKey().equals(FinanceReportTitles.NO.getKey())){
				cell.setCellValue(i);
			}else{
				cell.setCellValue(reportData.get(title_key.getKey()));
			}
			overDueOrderReport.autoSizeColumn(title_key.ordinal());
		}
	}

	private HSSFCellStyle create_excel_content_style(Workbook wb,
			HSSFCellStyle cellStyle) {
		HSSFFont fontForContent = (HSSFFont) wb.createFont();
		cellStyle.setFont(fontForContent);
		fontForContent.setFontName(ExcelHandlerSpec.FONT_SETTING_FOR_REPORT_CONTENT);
		fontForContent.setFontHeightInPoints(ExcelHandlerSpec.FONT_SIZE);
		HSSFCellStyle contentStyle = (HSSFCellStyle) wb.createCellStyle();
		return contentStyle;
	}

	private HSSFCellStyle create_excel_title(OverDueReportTitles[] titles,
			Workbook wb, Sheet overDueOrderReport) {
		HSSFFont font = (HSSFFont) wb.createFont();
		font.setFontName(ExcelHandlerSpec.FONT_SETTING_FOR_REPORT_TITLE);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font.setFontHeightInPoints(ExcelHandlerSpec.FONT_SIZE);
		HSSFCellStyle firstRowCellStyle = (HSSFCellStyle) wb.createCellStyle();
		HSSFCellStyle cellStyle = (HSSFCellStyle) wb.createCellStyle();
		firstRowCellStyle.setFont(font);
		Row row = overDueOrderReport.createRow((short) 0);
		for(ReportTitle key :titles){
			Cell cell = row.createCell(key.ordinal());
			create_special_style_for_title(wb, firstRowCellStyle, key, cell);
			overDueOrderReport.autoSizeColumn(key.ordinal());
		}
		return cellStyle;
	}

	private void create_special_style_for_title(Workbook wb,
			HSSFCellStyle firstRowCellStyle, ReportTitle key, Cell cell) {
		if(key.getKey().equals(OverDueReportTitles.OVERDUEDAYS.getKey())){
			HSSFFont fontColor = (HSSFFont) wb.createFont();
			fontColor.setFontName(ExcelHandlerSpec.FONT_SETTING_FOR_REPORT_TITLE);
			fontColor.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
			fontColor.setFontHeightInPoints(ExcelHandlerSpec.FONT_SIZE);
			fontColor.setColor(HSSFColor.RED.index);
			HSSFCellStyle colorStyle = (HSSFCellStyle) wb.createCellStyle();
			colorStyle.setFont(fontColor);
			cell.setCellStyle(colorStyle);
			cell.setCellValue(key.getKey());
		}else{
			cell.setCellStyle(firstRowCellStyle);
			cell.setCellValue(key.getKey());
		}
	}

}
